package com.njd.meitu.tv.main;

import java.util.ArrayList;
import java.util.LinkedList;

import com.njd.meitu.tv.R;
import com.njd.meitu.tv.image.AbImageLoader;
import com.njd.meitu.tv.model.MeiziTuInfo;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.ImageButton;

import uk.co.senab.photoview.PhotoView;

public class ViewPagerActivity extends Activity {
	private ArrayList<MeiziTuInfo> imgList;
	private int imgIndex;
	private SamplePagerAdapter mAdapter;
	
    @SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//设置ActionBar 浮动到view 上层来
		 requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);  

		 //设置半透明的底色
//		 getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_bg));
		
        setContentView(R.layout.activity_view_pager);
        ViewPager mViewPager = (HackyViewPager) findViewById(R.id.view_pager);
		setContentView(mViewPager);
		
		Bundle bundle = getIntent().getExtras();
		imgList = (ArrayList<MeiziTuInfo>)bundle.getSerializable("imgList");
		imgIndex = bundle.getInt("imgIndex");
		
		
		ActionBar actionBar = getActionBar();  
		actionBar.setTitle(imgList.get(imgIndex).getMsg());
	    actionBar.setDisplayHomeAsUpEnabled(true);  

		mAdapter = new SamplePagerAdapter(this, imgList);
		mViewPager.setAdapter(mAdapter);
		
		mViewPager.setCurrentItem(imgIndex);
		
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int index) {
				getActionBar().setTitle(imgList.get(index).getMsg());
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		
	}
    
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case android.R.id.home:
				finish(); 
				return true;
		}
		
		return super.onOptionsItemSelected(item);
	}

	static class SamplePagerAdapter extends PagerAdapter {
		private Context mContext;
		private ArrayList<MeiziTuInfo> imgList;
		private AbImageLoader mAbImageLoader = null;
		
		public SamplePagerAdapter(Context context, ArrayList<MeiziTuInfo> imgList) {
			mContext = context;
			this.imgList = imgList;
			mAbImageLoader = new AbImageLoader(mContext);
		}
		
		@Override
		public int getCount() {
			return imgList.size();
		}

		@Override
		public View instantiateItem(ViewGroup container, int position) {
			View convertView = null;
			ViewHolder holder = null;
			
			if (convertView == null) {
				LayoutInflater layoutInflator = LayoutInflater.from(container.getContext());
	            convertView = layoutInflator.inflate(R.layout.photo_view, null);
	            
	            holder = new ViewHolder();
	            holder.photoView = (PhotoView) convertView.findViewById(R.id.pv_photo);
	            holder.imageButtonPingtu = (ImageButton) convertView.findViewById(R.id.imageButton_pingtu);
	            
	            convertView.setTag(holder);
			}
			
			mAbImageLoader.setEmptyImage(R.drawable.image_empty);			
			mAbImageLoader.setErrorImage(R.drawable.image_error);
			mAbImageLoader.setLoadingView(convertView.findViewById(R.id.progressBar_PhotoView));
			mAbImageLoader.display(holder.photoView, imgList.get(position).getIsrc().replace("s_", ""));
			
			
			// Now just add PhotoView to ViewPager and return it
			container.addView(convertView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			
			holder.imageButtonPingtu.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
//					Intent intent = new Intent(mContext, PintuActivity.class);					
//					Bundle mBundle = new Bundle();  
//					mBundle.putParcelable("bitmap", bitmap);
//					intent.putExtras(mBundle);
//					mContext.startActivity(intent);
				}
			});
			
			return convertView;
		}
		
	    class ViewHolder {
	    	PhotoView photoView;
	    	ImageButton imageButtonPingtu;
//	        TextView contentView;
//	        TextView timeView;
	    }

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

	}

}
